<?php
/**
 * PHP versions 5
 */

/**
 * @author MARIA
 */
class Zend_Sql_Parser // implements Zend_Sql_Interface::toString
{
	/**
	 * @var Zend_Sql_Parser_Adapter_Abstract
	 */
	protected $adapter;
	/**
	 * @return Zend_Sql_Parser_Adapter_Abstract $adapter
	 */
	public function getAdapter() {
		return $this->adapter;
	}
	/**
	 * @param Zend_Sql_Adapter_Standard_Parser $adapter
	 */
	public function setAdapter(Zend_Sql_Adapter_Abstract_Parser $adapter) {
		$this->adapter = $adapter;
		return $this;
	}

	/**
	 * Constructor
	 *
	 * @param String|Zend_Db_Adapter_Abstract $db
	 * @param String|Zend_Db_Statement $query
	 *
	 * @param string $string the SQL query to parse
	 * @param string $dialect the SQL dialect
	 *
	 * @uses  Zend_Sql_Parser::setDialect()
	 * @uses  Zend_Sql_Parser::$lexer      W to create it
	 * @uses  Zend_Sql_Parser::$symbols    R
	 * @uses  Zend_Sql_Parser::$lexeropts  R
	 * @uses  Zend_Sql_Parser_Lexer        to create an Object
	 * @uses  Zend_Sql_Parser_Lexer::$symbols W to set it
	 * @uses  is_string()
	 */
	/*
	 public function __construct($db, $query = null)
	 {
		if ($db instanceof Zend_Config) {
		$db = $db->toArray();
		}
		if ($db instanceof Zend_Db_Adapter_Abstract) {
		$db = substr(get_class($db), strlen('Zend_Db_Adapter_'));
		}

		if (is_array($db)) {
		//$this->setOptions($db);
		} else if (is_string($db)) {
		$adapter = self::factory($db);
		$this->setAdapter($adapter);
		}

		if (is_string($query)) {
		//$this->statement = $this->parse($query);
		//->getStatement($query);
		}
		}
		*/

	/**
	 *
	 * @param $query
	 * @return Zend_Sql_Statement_Abstract
	 * @throws Zend_Sql_Exception
	 */
	public function parse($query=null) {
		$query = is_null($query) ? $this->query: $query;
		if (is_null($query)) {
			throw new Zend_Sql_Exception('Invalide parameter');
		}
		$adapter = $this->getAdapter();
		if (is_null($adapter)) {
			throw new Zend_Sql_Exception('Null adapter');
		}
		return $adapter->parse($query);
	}

	/**
	 * @param  string $type Pdo_MySql, MySqli
	 * @return Zend_Form
	 * @throws Zend_Form_Exception on invalid type
	 */
	static public function factory($type='Standard')
	{
		require_once 'Zend/Loader.php';
		 
		$dialect = 'Zend_Sql_Parser_Adapter_' . $type . '_Dialect';
		Zend_Loader::loadClass($dialect);
		$dialect = new $dialect();
		if (is_null($dialect)) {
			require_once 'Zend/Db/Sql/Exception.php';
			throw new Zend_Sql_Exception('No such File or Class dialect :"'.$type.'"');
		} // else autoload ?

		$lexer = 'Zend_Sql_Parser_Adapter_' . $type . '_Lexer';
		Zend_Loader::loadClass($lexer);
		$lexer = new $lexer(array('lookahead'=>3, 'allowIdentFirstDigit'=>true));
		if (is_null($lexer)) {
			require_once 'Zend/Db/Sql/Exception.php';
			throw new Zend_Sql_Exception('No such File or Class lexer :"'.$type.'"');
		} // else autoload ?
		$lexer->setLookahead(3);
		$lexer->setAllowIdentFirstDigit(true);
		//$lexer->setDialect($dialect);
		$lexer->setSymbols($dialect->getSymbols());
		$lexer->setComments($dialect->comments);
		$lexer->setQuotes($dialect->quotes);

		$adapter = 'Zend_Sql_Parser_Adapter_' . $type;
		Zend_Loader::loadClass($adapter);
		$adapter = new $adapter();
		if (is_null($adapter)) {
			require_once 'Zend/Db/Sql/Exception.php';
			throw new Zend_Sql_Exception('No such File or Class adapter :"'.$type.'"');
		}
		$adapter->setLexer($lexer);

		$parser = new self();
		$parser->setAdapter($adapter);

		return $parser;
	}
}
